﻿using System;
using System.Data;
using System.Configuration;
using System.Web;

using System.Text.RegularExpressions;

namespace UtilsBase
{
    /// <summary>
    /// 服务端数据验证
    /// </summary>
    public static class UtilsIsValid
    {
        /// <summary>
        /// 判断日期格式
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDate(string value)
        {
            try
            {
                DateTime DTTEST = Convert.ToDateTime(value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断数字有效性
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }
        /// <summary>
        /// 判断Guid有效性
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsGuid(string ID)
        {
            Guid userID = Guid.Empty;
            try
            {
                userID = new Guid(ID);
                return true;
            }
            catch
            {
                return false;
            }


        }

        /// <summary>
        /// 判断手机号有效性
        /// </summary>
        /// <param name="Mobile"></param>
        /// <param name="err"></param>
        /// <returns></returns>
        public static bool IsMobilePhone(string Mobile, out string err)
        {

            err = "";
            try
            {
                Mobile = Mobile.Trim();
                if (string.IsNullOrEmpty(Mobile))
                {
                    err = "手机号码不能为空";
                    return false;
                }
                else if (Mobile.Length != 11)
                {
                    err = "手机号码长度不正确";
                    return false;
                }
                string yidongflag = Mobile.Substring(0, 3);
                string[] strPhone = { "134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "188" };
                bool yidongflag_bool = false;
                for (int i = 0; i < strPhone.Length; i++)
                {
                    if (yidongflag == strPhone[i])
                    {
                        yidongflag_bool = true;
                    }
                }

                if (Regex.IsMatch(Mobile, @"^[+-]?\d*[.]?\d*$") && yidongflag_bool == true)
                {
                    return true;
                }
                else
                {
                    err = "手机号码不正确";
                    return false;
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }


        }
        /// <summary>
        /// 判断邮箱格式有效性
        /// </summary>
        /// <param name="mailno"></param>
        /// <returns></returns>
        public static bool IsEmail(string mailno)
        {
            bool bflag = Regex.IsMatch(mailno, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return bflag;
        }
    }
}
